#include "slepdef.h"
#include "slepblas.h"
#include "slep.h"

#include <stdio.h>
#include <assert.h>
#include <ctype.h>







slep_vector* slepReadDenseVector(char *filename){
	slep_vector* vctr =0;
	int i=0, cnt=0;
	FILE* fp =0;
	fp = fopen(filename,"r");
	assert(fp!=0);
	while(fscanf(fp,"%*lf")!=EOF)
		cnt++;
	assert(fseek(fp,0,SEEK_SET)==0);
	vctr = slepAllocDenseVector(cnt);
	while(i<cnt)
		fscanf(fp,"%lf",&(vctr->data.vctr[i++]) );
	assert(fclose(fp)==0);
	return vctr;
}



slep_matrix* slepReadDenseMatrix(char *filename){
	slep_matrix* mtrx =0;
	static char buffer[8192];
	int m=0,n=0;
	int i=0, cnt=0,j=0;
	FILE* fp =0;
	fp = fopen(filename,"r");
	assert(fp!=0);

	assert(fgets(buffer,8192,fp)!=0);
	m++;
	for(i=0; i<8192;){
		if(buffer[i]== 0 )
			break;
		while(isalnum(buffer[i]) || buffer[i]=='.')
			i++;
		n++;
		while(isspace(buffer[i]))
			i++;
	}


	while(fgets(buffer,8192,fp)!=0)
		m++;

	assert(fseek(fp,0,SEEK_SET)==0);

	mtrx = slepAllocDenseMatrix(m,n);

	for(i=0;i<m; i++)
		for(j=0;j<n; j++)
			fscanf(fp,"%lf",&(mtrx->data.mtrx[i+j*m]));
	assert(fclose(fp)==0);
	return mtrx;
}


void slepWriteDenseMatrix(char* filename, slep_matrix* mtrx){
	FILE* fp=0;
	int i,j;
	fp = fopen(filename,"w+");
	assert(fp!=0);

	for(i=0; i < mtrx->m; i++){
		for(j=0; j< (mtrx->n)-1; j++)
			fprintf(fp,"%lf ",mtrx->data.mtrx[i + j* mtrx->ld]);
		fprintf(fp,"%lf\n",mtrx->data.mtrx[i + j* mtrx->ld]);
	}
	assert(fclose(fp)==0);
}



void slepWriteDenseVector(char* filename, slep_vector* vctr){
	FILE* fp=0;
	int i;
	fp = fopen(filename,"w+");
	assert(fp!=0);

	for(i=0; i < vctr->dim; i++)
		fprintf(fp,"%lf\n",vctr->data.vctr[i]);
	assert(fclose(fp)==0);
}


void slepVerboseWriteDenseMatrix(char* filename, slep_matrix* mtrx){
	FILE* fp=0;
	int i,j;
	fp = fopen(filename,"w+");
	assert(fp!=0);
	fprintf(fp,"%dx%d matrix\n",mtrx->m, mtrx->n);
	fprintf(fp,"leading dimension = %d\n",mtrx->ld);
	fprintf(fp,(mtrx->sparsity==SlepDense ? ("sparsity = SlepDense\n"):("sparsity = SlepSparse\n")) );

	for(i=0; i < mtrx->m; i++){
		for(j=0; j< (mtrx->n)-1; j++)
			fprintf(fp,"%lf ",mtrx->data.mtrx[i + j* mtrx->ld]);
		fprintf(fp,"%lf\n",mtrx->data.mtrx[i + j* mtrx->ld]);
	}
	assert(fclose(fp)==0);
}



void slepVerboseWriteDenseVector(char* filename, slep_vector* vctr){
	FILE* fp=0;
	int i;
	fp = fopen(filename,"w+");
	assert(fp!=0);

	fprintf(fp, "%d dimensional\n", vctr->dim);
	fprintf(fp, "increment for elements = %d\n", vctr->inc);
	fprintf(fp,(vctr->sparsity==SlepDense ? ("sparsity = SlepDense\n"):("sparsity = SlepSparse\n")) );

	for(i=0; i < vctr->dim; i++)
		fprintf(fp,"%lf\n",vctr->data.vctr[i]);
	assert(fclose(fp)==0);
}



void slepVerbConsoleWrDMat(slep_matrix* mtrx){
	int i,j;
	printf("%dx%d matrix\n",mtrx->m, mtrx->n);
	printf("leading dimension = %d\n",mtrx->ld);
	printf((mtrx->sparsity==SlepDense ? ("sparsity = SlepDense\n"):("sparsity = SlepSparse\n")) );

	for(i=0; i < mtrx->m; i++){
		for(j=0; j< (mtrx->n)-1; j++)
			printf("%lf ",mtrx->data.mtrx[i + j* mtrx->ld]);
		printf("%lf\n",mtrx->data.mtrx[i + j* mtrx->ld]);
	}
}



void slepVerbConsoleWrDVec(slep_vector* vctr){
	int i;

	printf( "%d dimensional\n", vctr->dim);
	printf( "increment for elements = %d\n", vctr->inc);
	printf((vctr->sparsity==SlepDense ? ("sparsity = SlepDense\n"):("sparsity = SlepSparse\n")) );

	for(i=0; i < vctr->dim; i++)
		printf("%lf\n",vctr->data.vctr[i]);

}